<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>备忘录</title>
    <link rel="stylesheet" type="text/css" href="./css/index.css" />
</head>

<body>
    <!-- 主体区域 -->
    <section id="todoapp">
        <!-- 输入框 -->
        <header class="header">
            <h1>备忘录</h1>
            <!-- 双向绑定输入内容 ， 且回车添加li项 -->
            <input v-model="newTodo" @keyup.enter="addTodo" placeholder="请输入任务" autofocus="autofocus" autocomplete="off"
                class="new-todo" />
        </header>
        <!-- 列表区域 -->
        <section class="main">
            <ul class="todo-list">
                <!-- 循环展示所有的li项 -->
                <li v-for="(todo, index) in todos" :key="index" class="todo">
                    <div class="view">
                        <!-- 显示索引+1为序号 -->
                        <span class="index">{{ index + 1 }}</span>
                        <!-- 显示项的内容 -->
                        <label>{{ todo }}</label>
                        <!-- 移除当前项 -->
                        <button class="destroy" @click="removeTodo(index)"></button>
                    </div>
                </li>
            </ul>
        </section>
        <!-- 统计和清空 -->
        <footer class="footer" v-if="todos.length > 0">
            <span class="todo-count">
                <!-- 统计总计数 -->
                <strong>{{ todos.length }}</strong> 项任务
            </span>
            <!-- 清除所有项 -->
            <button class="clear-completed" @click="clearTodos">
                Clear
            </button>
        </footer>
    </section>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
        var app = new Vue({
            el: "#todoapp", 
            data: {
                newTodo: "", // 输入的新任务内容
                todos: [] // 存储所有任务的数组
            },
            methods: {
                // 添加任务
                addTodo() {
                    if (this.newTodo.trim() !== "") {
                        this.todos.push(this.newTodo.trim());
                        this.newTodo = ""; // 清空输入框
                    }
                },
                // 删除任务
                removeTodo(index) {
                    this.todos.splice(index, 1);
                },
                // 清空所有任务
                clearTodos() {
                    this.todos = [];
                }
            }
        });
    </script>
</body>

</html>